<?php

/**
 * 前台 首页 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: common.class.php 109 2012-03-15 12:22:59Z zwt007 $
 * http://www.t-mac.org；
 */
class common extends Model
{

    private $db;
    private $article_table;
    private $category_table;
    private $add_category_table;
    private $ad_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->article_table = DB_PREFIX . 'article';
        $this->category_table = DB_PREFIX . 'category';
        $this->add_category_table = DB_PREFIX . 'addoncategory';
        $this->ad_table = DB_PREFIX . 'ad';
    }

    /**
     * 取全站公共部分
     * @return type 
     */
    public function getCommon()
    {
        //取config配置文件
        $configcache = Tmac::config('configcache.config', APP_NAME, '.inc.php');
        $configcache['cfg_powerby'] = $this->HD($configcache['cfg_powerby']);

        $this->assign('config', $configcache);
        return $configcache;
    }

    /**
     * 从数据库中读取广告
     * @param <type> $id
     * @param <type> $css
     */
    public function ShowAD($ad_name)
    {
        $sql = "SELECT * FROM $this->ad_table WHERE ad_name = '$ad_name' AND ad_state_radio<>3 ORDER BY ad_order DESC, ad_id DESC LIMIT 0,1";
        $rs = $this->db->cacheGetRow($sql, 86400);
        if ($rs) {
            //如果状态是正常 就开始判断开始和结束时间
            if ($rs['ad_state_radio'] == 1) {
                if ($rs['ad_starttime'] > time() || $rs['ad_endtime'] < time()) {
                    $ad_body = null;
                    $this->assign($ad_name, $ad_body);
                    return $ad_body;
                }
            }
            //取config配置文件
            $configcache = Tmac::config('configcache.config', APP_NAME, '.inc.php');
            switch ($rs['ad_type_radio'])
            {
                case 1:
                    $ad_body = "<a href='{$rs['ad_link']}' target='_blank' title='{$rs['ad_title']}'>{$rs['ad_title']}</a>";
                    break;
                case 2:
                    $ad_body = "<a href='{$rs['ad_link']}' target='_blank' title='{$rs['ad_title']}'><img width='{$rs['ad_width']}' height='{$rs['ad_height']}' src='{$configcache['cfg_indexurl']}{$rs['ad_uploadfile']}' border='0' alt='{$rs['ad_title']}' /></a>";
                    break;
                case 3:
                    $ad_body = "{$rs['ad_externallinks']}";
                    break;
            }
        } else {
            $ad_body = null;
        }

        return $ad_body;
    }

}